<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</body>
<script>
  "use strict"; {
    let jsons = {
      "name": "cz",
      "age": 19,
      "gender": true,
      "singer": "刘德华",
      "song": "冰雨"
    };
    let expText = `name === "cz" && age == 18 And gender = true or singer != "刘华"　|| song !== "冰雨"`;
    const checkDataExpText = (data = {}, expText) => {
      if (JSON.stringify(data) === "{}") return `数据源为空`;
      // 1先对表达式的空格除去，使用正则
      expText = expText.replace(/\s/ig, "");
      // 2对表达式进行切割(支持&& || and or)
      let expText_arr = expText.split(/&&|\|\||and|or/gi);
      // 3对所有的逻辑运算符进行提取
      let expText_math = expText.match(/&&|\|\||and|or/gi);
      // ４得到所有的判断处理结果
      let judge_res = expText_arr.reduce((pre, value, index, arr_pre) => {
        // 且按 == 或=对每一个进行切割
        let [names, values] = value.split(/===|==|=|!==|!=/);
        // 判断数据类型,若是支付串则给其添加上"",再进行比较
        if (data[names].constructor === String) data[names] = `"${data[names]}"`;
        // 若出现取反时，则需要分开判断
        if (/!==|!=/.test(value)) {
          pre.push(data[names].toString() != values ? true : false);
        } else {
          pre.push(data[names].toString() == values ? true : false);
        };
        return pre;
      }, []);
      console.table(expText_math);
      console.table(judge_res);
      // 当结果只有一个时，直接返回
      if (judge_res.length == 1) return judge_res[0];
      // 当大于1时才进行比较
      for (let i = 0; i < judge_res.length; i++) {
        // 最后一个时直接把它返回出去即可
        if (i == judge_res.length - 1) return judge_res[i];
        if (/&&|and/.test(expText_math[i])) judge_res[i + 1] = judge_res[i] && judge_res[i + 1];
        if (/\|\||or/.test(expText_math[i])) judge_res[i + 1] = judge_res[i] || judge_res[i + 1];
      };
    };
    let i = checkDataExpText(jsons, expText);
    console.log(i);
  };
  if (true && false && true || true || false) {
    console.log(`对的`);
  } else {
    console.log(`错的`);
  };
</script>

</html>